package com.baidu.dao.impl;

import com.baidu.common.bean.GoodsType;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.JdbcConstant;
import com.baidu.dao.TypeDao;
import com.baidu.dao.abstractclass.BaseAbstract;
import com.mysql.jdbc.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/20/15:34
 */
public class TypeDaoImpl extends BaseAbstract<GoodsType> implements TypeDao {
    public TypeDaoImpl() throws WebException {}

    @Override
    public GoodsType getTypeByName(String name) throws WebException{
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.TYPE_QUERYBYNAME));
            preparedStatement.setString(1,name);
            resultSet = preparedStatement.executeQuery();
            String id = "";
            String typeName = "";
            if (resultSet.next()){
                id = resultSet.getString("id");
                typeName = resultSet.getString("name");
            }

            GoodsType type = new GoodsType();


            if (StringUtils.isNullOrEmpty(id)){
                type.setName(name);
                int typeId = add(type);

                type.setId(typeId);
                type.setName(name);
            }else{
                type.setId(Integer.parseInt(id));
                type.setName(typeName);
            }
            return type;
        } catch (WebException | SQLException e) {
            e.printStackTrace();
            throw  new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(preparedStatement,resultSet);
        }
    }

    @Override
    public List<GoodsType> queryGoodsTypeList() throws WebException {
        PreparedStatement queryListStatement = null;
        ResultSet typeResultSet = null;
        try {
            String sql = getPropertiesString(JdbcConstant.TYPE_QUERY_ALL);
            queryListStatement = connection.prepareStatement(sql);
            typeResultSet = queryListStatement.executeQuery();
            List<GoodsType> typeList = new ArrayList<>();
            while (typeResultSet.next()){
                Integer id = typeResultSet.getInt("id");
                String name = typeResultSet.getString("name");

                GoodsType type = new GoodsType();
                type.setId(id);
                type.setName(name);
                typeList.add(type);
            }
            return typeList;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(queryListStatement,typeResultSet);
        }
    }

    @Override
    public GoodsType get(Integer id) {
        return null;
    }

    @Override
    public int update(GoodsType type)  {
        return 0;
    }

    @Override
    public int delete(Integer integer) {
        return 0;
    }

    @Override
    public int add(GoodsType type) throws WebException {
        PreparedStatement preparedStatement = null;
        ResultSet idResultSet = null;
        try {
            preparedStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.TYPE_INSERT_BEAN),Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setInt(1,0);
            preparedStatement.setString(2,type.getName());
            preparedStatement.executeUpdate();

            idResultSet = preparedStatement.getGeneratedKeys();
            int id = 0;
            while (idResultSet.next()){
                id = idResultSet.getInt(1);
            }

            connection.commit();
            return id;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(preparedStatement,idResultSet);
        }
    }
}
